import { OrderType } from '@/enums'
import {
  cancelOrder,
  delOrder,
  followOrUnfollow,
  getPerInfo
} from '@/services/consult'
import { getMedicalOrderDetail } from '@/services/order'
import { sendCode } from '@/services/user'
import type { ConsultOrderItem, FollowType } from '@/types/consult'
import type { OrderDetail } from '@/types/order'
import type { SendCodeType } from '@/types/user'
import {
  showFailToast,
  showImagePreview,
  showSuccessToast,
  type FormInstance
} from 'vant'
import { onUnmounted, type Ref } from 'vue'
import { ref } from 'vue'

// 组合式API封装的业务函数, Hook函数  推荐 hook函数 使用useXXX
// 关注的hook函数
export function useFollow(type: FollowType) {
  const loading = ref(false)
  // 关注/取消关注 医生
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    // 1. 开启loading
    loading.value = true
    // 2. 发送关注/取消关注的请求
    await followOrUnfollow({ type, id: item.id })
    // 3. 更新视图
    item.likeFlag = item.likeFlag === 1 ? 0 : 1
    // 4. 关闭loading
    loading.value = false
  }
  return { follow, loading }
}
// 查看处方的hook函数
export const useShowPreInfo = () => {
  // 展示预览处方
  const showPreInfo = async (id: string) => {
    if (id) {
      const res = await getPerInfo(id)
      showImagePreview([res.data.url])
    }
  }
  return showPreInfo
}

// 取消问诊的hook函数
export const useCancel = () => {
  // 取消问诊
  const cancelLoading = ref(false)
  const cancel = async (item: ConsultOrderItem) => {
    cancelLoading.value = true
    try {
      //  发送取消的请求
      await cancelOrder(item.id)
      // 2. 将item的状态修改
      item.status = OrderType.ConsultCancel
      // 3. 修改状态文字
      item.statusValue = '已取消'
      // 4. 提示 取消成功
      showSuccessToast('取消成功')
    } catch {
      showFailToast('取消失败')
    } finally {
      cancelLoading.value = false
    }
  }
  return { cancelLoading, cancel }
}

// 删除订单的hook函数
export const useDel = (cb: () => void) => {
  // 删除订单
  const delLoading = ref(false)
  const del = async (item: ConsultOrderItem) => {
    try {
      delLoading.value = true
      // 发送请求
      await delOrder(item.id)
      // 执行传递进来的函数
      cb && cb()
      // 提示
      showSuccessToast('删除成功')
    } catch {
      showFailToast('删除失败')
    } finally {
      delLoading.value = false
    }
  }
  return { delLoading, del }
}

// 获取订单详情
export const useMedicalDetail = (id: string) => {
  const order = ref<OrderDetail>()
  // 获取药品订单详情
  const getOrderDetail = async () => {
    const res = await getMedicalOrderDetail(id)
    order.value = res.data
  }
  getOrderDetail()
  return order
}

// 倒计时hook
export const useCountDown = (mobile: Ref<string>, type: SendCodeType) => {
  let timeId = -1
  const time = ref(0)
  const fromRef = ref<null | FormInstance>(null)
  // 发送验证码
  const getCode = async () => {
    if (time.value > 0) return // 阻止在倒计时期间再次发送验证码
    // 1. 校验手机号是否合法  validate()中不写内容校验所有表单项, 写对应表单项的name指定校验
    await fromRef.value?.validate('mobile')
    // 2. 发送验证码
    await sendCode(mobile.value, type)
    // 3. 提示
    showSuccessToast('验证码已发送')
    // 4. 倒计时
    clearInterval(timeId)
    time.value = 60
    timeId = setInterval(() => {
      if (time.value <= 0) return clearInterval(timeId)
      time.value--
    }, 1000)
  }
  // 组件销毁后(离开页面后倒计时如果没有结束手动清除)
  onUnmounted(() => {
    clearInterval(timeId)
  })
  return {
    getCode,
    time,
    fromRef
  }
}
